package dao;

import bean.Project;
import tool.HibernateSessionFactory;

import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.query.Query;

import java.util.List;

import javax.persistence.criteria.CriteriaQuery;

public class ProjectDAOImpl implements ProjectDAO {
    private Session session;
    private Transaction transaction;
    private boolean completed;

    public ProjectDAOImpl(Session session) {
        this.session = session;
    }

    @Override
    public boolean add(Project project) {
        try {
            transaction = session.beginTransaction();
            session.save(project);
            completed = true;
        } catch (Exception e) {
            completed = false;
            e.printStackTrace();
        } finally {
            transaction.commit();
        }
        return completed;
    }

    @Override
    public boolean modify(Project temporary) {
        try {
            transaction = session.beginTransaction();
            Project project = session.get(Project.class, temporary.getProjectId());
            project.setProjectName(temporary.getProjectName());
            project.setDuration(temporary.getDuration());
            project.setInvestment(temporary.getInvestment());
            project.setStartTime(temporary.getStartTime());
            session.update(project);
            completed = true;
        } catch (Exception e) {
            completed = false;
            e.printStackTrace();
        } finally {
            transaction.commit();
        }
        return completed;
    }

    @Override
    public boolean remove(int projectId) {
        try {
            transaction = session.beginTransaction();
            Project project = session.get(Project.class, projectId);
            session.delete(project);
            completed = true;
        } catch (Exception e) {
            completed = false;
            e.printStackTrace();
        } finally {
            transaction.commit();
        }
        return completed;
    }

    @Override
    public List<Project> queryAll() {
        List<Project> projects = null;
        try {
//            Query<Project> query = session.createQuery("FROM Project");
//            projects = query.list();
            CriteriaQuery<Project> cq = session.getCriteriaBuilder().createQuery(Project.class);
            // 获取根实体
            cq.from(Project.class);
            projects = session.createQuery(cq).list();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return projects;
    }

    @Override
    public List<Project> queryByProjectName(String keyword) {
        List<Project> projects = null;
        try {
            @SuppressWarnings("unchecked")
            Query<Project> query = session.createQuery("SELECT project FROM Project AS project WHERE project.projectName LIKE :projectName");
            query.setParameter("projectName", "%" + keyword + "%");
            projects = query.list();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return projects;
    }

    @Override
    public List<Project> queryByProjectId(int projectId) {
        List<Project> projects = null;
        try {
            session = HibernateSessionFactory.getSession();
            transaction = session.beginTransaction();
//            Query<Project> query = session.createQuery("FROM Project project WHERE project.projectId=" + projectId);
//            projects = query.list();
            CriteriaQuery<Project> cq = session.getCriteriaBuilder().createQuery(Project.class);
            // 获取根实体
            cq.from(Project.class);
            projects = session.createQuery("SELECT project FROM Project AS project WHERE project.projectId=" + projectId, Project.class).list();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            transaction.commit();
            session.close();
        }
        return projects;
    }
}
